package com.hpin.mlxteacher.child;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.adapter.RecordAdapter;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.BabyRecordRes;
import com.hpin.mlxteacher.bean.RecordData;
import com.hpin.mlxteacher.utils.AbDateUtil;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.HttpUtil;
import com.hpin.mlxteacher.widget.GridViewForScrollView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 宝宝生活记录页面
 * Created by Aaron on 2015/7/28.
 */
public class LifeRecordActivity extends BaseActivity {

    private static final int RECODER_CODE = 10801;
    private List<RecordData> recordDatas;
    private RecordAdapter recordAdapter;
    private int modifyPosition = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_life_record);

        initWidget();

        //获取宝宝生活记录
        getBabyRecord();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.baby_life_record));

        iv_left.setOnClickListener(this);

        GridViewForScrollView gv_baby = (GridViewForScrollView) findViewById(R.id.gv_baby);

        recordDatas = new ArrayList<RecordData>();
        recordAdapter = new RecordAdapter(this, recordDatas);

        gv_baby.setAdapter(recordAdapter);

        gv_baby.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                //生活记录
//                if (recordDatas.get(position).check == 1) {   //如果已经点评过了
//
//                    new AlertDialog.Builder(LifeRecordActivity.this).setTitle("提示").setMessage("您已经对" + recordDatas.get(position).stuName + "同学点评过了").setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialogInterface, int i) {
//                            dialogInterface.dismiss();
//                        }
//                    }).show();
//
//                }else {     //未点评

                Intent intent = new Intent(LifeRecordActivity.this, RecordDetailActivity.class);
                intent.putExtra("stuName", recordDatas.get(position).childName);
                intent.putExtra("stuId", recordDatas.get(position).id);
//                intent.putExtra("stuId", recordDatas.get(position).stuId);
                intent.putExtra("check", recordDatas.get(position).state);//是否已经提交过
//                    intent.putExtra("position", position);
                modifyPosition = position;
                startActivityForResult(intent, RECODER_CODE);

//                }

            }
        });

    }

    /**
     * 获取宝宝生活记录
     */
    private void getBabyRecord() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.token, ""));
        j.put("day", AbDateUtil.getStringByFormat(new Date(), AbDateUtil.dateFormatYMD));
        j.put("classId", sp.getString(Constant.classId, ""));

        HttpUtil.setPostRequest("获取中...", this, Constant.WEB_ROOT + Constant.RECORDLIST
                , j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    BabyRecordRes babyRecordRes = JSONObject.parseObject(str, BabyRecordRes.class);

                    if (babyRecordRes.code == Constant.SUCCESS) {

                        recordDatas = babyRecordRes.data;

                        recordAdapter.setDatas(recordDatas);
                        recordAdapter.notifyDataSetChanged();

                    } else {
                        showToast(Constant.showErrorMsg(babyRecordRes.code, LifeRecordActivity.this));
                    }

                } catch (Exception e) {

                    e.printStackTrace();
                    showToast(Constant.NODATA);
                }

            }
        }, null);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data == null)
            return;

        if (requestCode == RECODER_CODE) {

            int check = data.getIntExtra("check", -1);

            if (check == 1) {

                recordDatas.get(modifyPosition).state = 1;
                recordAdapter.setDatas(recordDatas);
                recordAdapter.notifyDataSetChanged();

            }

        }

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());
    }

}
